<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Middleware;

use App\Exceptions\ApiException;
use App\Services\AdminAuthService;
use Closure;
use Illuminate\Http\Request;

class AdminAuth
{
    /**
     * Handle an incoming request.
     *
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
//        return $next($request);
        $actionName = $request->route()->getActionName();
        $userId = $request->input('user_id');
        $service = new AdminAuthService();
        $check = $service->checkAuth($userId, $actionName);
        if (! $check) {
            throw new ApiException('没有权限访问', 1);
        }
        return $next($request);
    }
}
